/**create by liuhua at 2020年5月18日 下午5:55:59**/
package com.star.aries.dto;

import com.fasterxml.jackson.annotation.JsonView;
import com.star.aries.constant.ErrorCodeEnum;

import lombok.Data;

@Data
public class BaseResult<T> {

  @JsonView(BaseResultView.class)
  private int code;
  @JsonView(BaseResultView.class)
  private String msg;
  @JsonView(BaseResultView.class)
  private String traceId;
  @JsonView(BaseResultView.class)
  private T data;

  public interface BaseResultView {

  }

  public BaseResult(T data) {
    this.code = ErrorCodeEnum.SUCCESS.getCode();
    this.msg = ErrorCodeEnum.SUCCESS.getMsg();
    this.data = data;
  }

  public BaseResult() {
    this.code = ErrorCodeEnum.SUCCESS.getCode();
    this.msg = ErrorCodeEnum.SUCCESS.getMsg();
  }

  public BaseResult(int code, String msg) {
    this.code = code;
    this.msg = msg;
  }

  public static BaseResult<Void> SYSTEM_ERROR() {
    return SYSTEM_ERROR(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
  }

  public static <T> BaseResult<T> SYSTEM_ERROR(String msg) {
    return SYSTEM_ERROR(ErrorCodeEnum.SYSTEM_ERROR.getCode(), msg);
  }
  
  public static <T> BaseResult<T> SYSTEM_ERROR(int code, String msg) {
    BaseResult<T> r = new BaseResult<>();
    r.setCode(code);
    r.setMsg(msg);
    return r;
  }

  public static <T> BaseResult<T> fail(int code, String msg) {
    BaseResult<T> r = new BaseResult<>();
    r.setCode(code);
    r.setMsg(msg);
    return r;
  }

  public static <T> BaseResult<T> success() {
    return new BaseResult<>();
  }

  public static <T> BaseResult<T> success(T t) {
    BaseResult<T> r = new BaseResult<>();
    r.setData(t);
    return r;
  }
}